package com.cfpamf.ms.insur.pay.service;

import com.cfpamf.common.ms.exception.MSNONeedRetryException;
import com.cfpamf.ms.insur.pay.config.BizSystemProperties;
import com.cfpamf.ms.insur.pay.constant.BizExceptEnum;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 获取各个系统的签名key
 * Create By zhengjing on 2019-10-21 16:01
 */
@Service
@EnableConfigurationProperties(BizSystemProperties.class)
@Getter
@Setter
public class SignKeyService {

    private final Map<String, String> keyMap;

    @Autowired
    public SignKeyService(BizSystemProperties bizSystemProperties) {
        keyMap = new ConcurrentHashMap<>(bizSystemProperties.getKeyMap());
    }


    /**
     * 获取签名的key
     *
     * @param systemCode
     * @return
     */
    public String getKeyBySys(String systemCode) {
        Assert.hasText(systemCode, "系统编码不能为空！");
        String s = keyMap.get(systemCode);
        if (StringUtils.isEmpty(s)) {
            throw new MSNONeedRetryException(BizExceptEnum.SYSTEM_CONFIG_ERROR.getCode(),
                    "商户系统编号【" + systemCode + "】配置不存在");
        }
        return s;
    }

}
